package com.ymatou.iapi.dglogin.testcase;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dglogin.parameter.AppSourceEnum;
import com.ymatou.iapi.dglogin.parameter.WxLoginBean;
import com.ymatou.iapi.dglogin.service.WxLoginCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.WxComponentService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.DGThirdPartyBindInfo;
import com.ymttest.database.model.DGUserExtInfo;
import com.ymttest.database.model.DGUserInfo;
import com.ymttest.database.model.DGWxUnionidBindInfo;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.database.sqlwapper.DGUserWapper;
/**
*用户微信联登
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_WxLogin{
	private static WxLoginBean wxloginBean;
	private static WxLoginCall wxloginCall;
	DGUserWapper DGUserWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户微信联登");
	}

	@Before
	public void caseUp() {
		wxloginBean=new WxLoginBean();
		wxloginCall=new WxLoginCall();
		DGUserWapper=new DGUserWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(wxloginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_WxLogin_001() {
		Logger.start(true,"用户微信联登成功");
		try {
//			String openId="oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000;
//			String unionId="uid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000;
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			int userid = DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
			//已有uid 不需要返回uid false
			Logger.verifyEquals("false",wxloginCall.getString("needUpdateUnionId"), "验证needUpdateUnionId");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(wxloginCall.getString("userId")));
			Logger.comment(token.toString());
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
			Logger.verifyEquals(u.getOpenId(),wxloginCall.getString("openId"), "验证openId");
			Logger.verifyEquals(u.getUnionId(),wxloginCall.getString("unionId"), "验证unionId");
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_002() {
		Logger.start(true,"已注册买家用户微信联登,成功,更新最后登入时间返回token设备号");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setUnionId(null);
			int userid = DGUserCallService.UserCreateCall(u);
			DGUserInfo dgUserInfo = DGUserWapper.selectDGUserInfo(userid).get(0);
			Date lt1 = dgUserInfo.getLastLoginTime();
			Thread.sleep(1100);
			String newuid="uid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000;
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",newuid);
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
			Logger.verifyNotNull(wxloginCall.getString("userId"), "验证suuserIdccess");
			Logger.verifyNotNull(wxloginCall.getString("accessToken"), "验证accessToken");
			Logger.verifyNotNull(wxloginCall.getString("cookieId"), "验证cookieId");
			Logger.verifyNotNull(wxloginCall.getString("deviceId"), "验证deviceId");
			dgUserInfo = DGUserWapper.selectDGUserInfo(userid).get(0);
			Date lt2 = dgUserInfo.getLastLoginTime();
			Logger.verifyNotEquals(lt1, lt2, "更新时间");
		}catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_003() {
		Logger.start(true,"已注册商家用户微信联登,成功");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
			Logger.verifyNotNull(wxloginCall.getString("userId"), "验证suuserIdccess");
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_004() {
		Logger.start(true,"已注册客服用户微信联登,成功");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setIscs(true);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
			Logger.verifyNotNull(wxloginCall.getString("userId"), "验证suuserIdccess");
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_005() {
		Logger.start(true,"不输入userType失败");
		try {
			//这个场景是用户用unionid1 注册 商家 然后登入的时候不输入usertype(默认0) 登入不匹配(此时需要重新注册买家账号才能登入成功)
			//前置条件是usertype不输入而且mock的openid为null--如果有openid则直接找openid登入
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
//			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",null);
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_006() {
		Logger.start(true,"不输入jsCode失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
//			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	//待确认 clientIp是否必传
	public void Tc_WxLogin_007() {
		Logger.start(true,"不输入clientIp失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
//			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_008() {
		Logger.start(true,"不输入appSource失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
//			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_009() {
		Logger.start(true,"不输入terminalSource成功");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
//			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			
		}catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_010() {
		Logger.start(true,"用户微信联登,openid不匹配(数据库不存在),unionid为空登入失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",null);
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			Logger.verifyEquals("100", wxloginCall.getString("errorCode"), "验证errorCode");
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_011() {
		Logger.start(true,"用户微信联登,openid不匹配,unionid匹配到已注册用户,绑定openid并登入成功(在其他appsource中注册)");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
//			u.setMobile(TestDataManager.getPhoneNum());
//			u.setMobileAreaCode("86");
//			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId()+1);
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
			List<DGThirdPartyBindInfo> dgbindlist = DGUserWapper.selectDGThirdPartyBindInfo(userid);
			System.out.println(FormatUtil.beanToGSONString(dgbindlist));
			Logger.verifyEquals(2, dgbindlist.size(), "记录2条绑定关系");
			Logger.verifyEquals(1l,dgbindlist.stream().filter(dg->dg.getOpenId().equals(u.getOpenId()+1)).count(), "记录中绑定了联登的openid");
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_012() {
		Logger.start(true,"用户微信联登,openid不匹配,unionid不匹配失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
//			u.setMobile(TestDataManager.getPhoneNum());
//			u.setMobileAreaCode("86");
//			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId()+1);
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId()+1);
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_013() {
		Logger.start(true,"输入appSource和openid不匹配失败");
		try {
			//用户用了appSource64 op1注册 用了appSource63 op1登入失败
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName("DG_SellerWxApp");
			wxloginBean.setAppSource(62);
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",null);
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_014() {
		Logger.start(true,"不输入WxAppName失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
//			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_015() {
		Logger.start(true,"已注册客服用户微信联登,普通用户登入失败-需要注册");
		try {
			//注册了客服但是没有注册普通账号
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setIscs(true);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(DGUserCallService.DG_BuyerWxApp);
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(0);
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			Logger.verifyEquals("100", wxloginCall.getString("errorCode"), "验证errorCode");
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_016() {
		Logger.start(true,"已注册客服用户微信联登,商家登入-客服登入成功");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setIscs(true);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			wxloginBean.setAppSource(DGUserCallService.DG_SellerApp);
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(1);
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_WxLogin_017() {
		Logger.start(true,"用户微信联登成功用户没有uid验证needUpdateUnionId");
		try {
//			String openId="oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000;
//			String unionId="uid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000;
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setUnionId(null);
			int userid = DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
			//已有uid 不需要返回uid false
			Logger.verifyEquals("false",wxloginCall.getString("needUpdateUnionId"), "验证needUpdateUnionId");
			Logger.verifyEquals(true,wxloginCall.getString("loginSession").length()>1,"验证loginSession有数据");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(wxloginCall.getString("userId")));
			Logger.comment(token.toString());
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WxLogin_018() {
		Logger.start(true,"已删除商家用户微信联登,失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			u.setUserType(1);
			int userid=DGUserCallService.UserCreateCall(u);
			DGUserCallService.DeleteUser(userid);
			wxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_WxLogin_019() {
		Logger.start(true,"用户微信联登-独立商户小程序托管-用户绑定信息不存在");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
            String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
            WxComponentService.deleteMchinfo(mchid);
            String appid = WxComponentService.ConfirmAuthorization(mchid);
            WxComponentService.AuthorizationCallBack(appid, "authorized");
//			String openId="oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000;
//			String unionId="uid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000;
            //这里的场景应该是belongSellerid不匹配 所以没查到
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setUnionId(null);
			int userid = DGUserCallService.UserCreateCall(u);
			wxloginBean.setWxAppName(mchid.toString());
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getComponentLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("false", wxloginCall.getString("success"), "验证success");
			Logger.verifyEquals("100", wxloginCall.getString("errorCode"), "验证errorCode");
			//"errorMessage":"用户绑定信息不存在"
			
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_WxLogin_020() {
		Logger.start(true,"用户微信联登-独立商户小程序托管-成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
            String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
            WxComponentService.deleteMchinfo(mchid);
            String appid = WxComponentService.ConfirmAuthorization(mchid);
            WxComponentService.AuthorizationCallBack(appid, "authorized");
//			String openId="oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000;
//			String unionId="uid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000;
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setBelongSellerId(mchid);
			int userid = DGUserCallService.UserCreateCall(u);
//			DGUserCallService.UserBind(userid, u.getUnionId(),mchid.toString());
			wxloginBean.setWxAppName(mchid.toString());
			wxloginBean.setAppSource(u.getAppSource());
			wxloginBean.setClientIp(TestDataManager.getRandomIp());
			wxloginBean.setJsCode("autotestjscode");
			wxloginBean.setTerminalSource(u.getTerminalSource());
			wxloginBean.setUserType(u.getUserType());
			wxloginCall.addHeader("mock", "mock-enable");
			wxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			wxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getComponentLoginSession-Normal");
			wxloginCall.addHeader("mockParam-1-sessionKey",DGUserCallService.SESSIONKEY);
			wxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			wxloginCall.addHeader("mockParam-1-unionId",u.getUnionId());
			wxloginCall.setData(wxloginBean);
			wxloginCall.callService();			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
			//已有uid 不需要返回uid false
			Logger.verifyEquals("false",wxloginCall.getString("needUpdateUnionId"), "验证needUpdateUnionId");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(wxloginCall.getString("userId")));
			Logger.comment(token.toString());
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			
			Logger.verifyEquals("true", wxloginCall.getString("success"), "验证success");
		} catch (Throwable e) {
			Logger.fail(new Exception(e));
		}
	}
}